package com.sunrun.sunrunframwork.uiutils;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;
import com.sunrun.sunrunframwork.BaseApplication;

/**
 * Toast工具类,封装Toast
 * Created by WQ on 2017/4/20.
 */

public class ToastUtils {
    private static Handler handler=new Handler(Looper.getMainLooper());
    private  static Toast shortToast = Toast.makeText(BaseApplication.getInstance(), "", Toast.LENGTH_SHORT);

    /**
     * 需要在主线程中调用一次该工具类,否则若直接在子线程中调用,会出现异常,
     * 因为Handler没有被正常初始化
     */
    public static void init(){

    }
    /**
     * 显示短Toast
     *
     * @param msg
     */
    public static void shortToast(Object msg) {
        showToast(msg,Toast.LENGTH_SHORT);
    }

    /**
     * 显示长Toast
     *
     * @param msg
     */
    public static void longToast(Object msg) {
        showToast(msg,Toast.LENGTH_LONG);
    }


  private   static void showToast(final Object msg,final int duration){
        if(Looper.getMainLooper()==Looper.myLooper()){
            showToastImpl(msg, duration);
        }else {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    showToastImpl(msg, duration);
                }
            });
        }
    }
    private   static void showToastImpl(Object msg, int duration) {
        if (msg instanceof Integer) {
            shortToast.setText((Integer) msg);
        } else {
            shortToast.setText(String.valueOf(msg));
        }
        shortToast.setDuration(duration);
        shortToast.show();
    }
}
